<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/23
 * Time: 下午3:37
 */
class Cost_center_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
    }


    public function query_cost_center_list($customer_id, $name = '')
    {
        $identity = $this->db->query("select identity from app_customer WHERE id = " . $customer_id)->row_array()['identity'];
        $requst = $_REQUEST;
        if ($identity == 1) {
            $sql = "SELECT * FROM app_cost_center WHERE customer_id = " . $customer_id;
            if ($name) {
                $sql .= " and name like '%$name%' ";
            }
            // 排序
            $sql .= " order by id desc";
            // 分页
            $desc['page'] = isset($requst['page']) ? (int)$requst['page'] : 1;
            $desc['psize'] = isset($requst['psize']) ? (int)$requst['psize'] : 10;
            $limit_from = ($desc['page'] - 1) * $desc['psize'];
            $sql .= " limit " . $limit_from . "," . $desc['psize'];
            $cost_center_list = $this->db->query($sql)->result_array();
        } else {
            $sql = "select * FROM app_cost_center as acc WHERE acc.id = (SELECT cost_center_id from app_customer WHERE id = $customer_id)";
            $cost_center_list = $this->db->query($sql)->row_array();
        }


        return $cost_center_list;
    }

    public function query_manager_list($customer_id, $cost_center_id, $username = '')
    {
        $requst = $_REQUEST;
        $identity = $this->db->query("select identity from app_customer WHERE id = " . $customer_id)->row_array()['identity'];
        if ($identity == 1)
        {
            $sql = "SELECT * FROM app_customer WHERE identity = 2 AND parent_id = $customer_id";
            if ($cost_center_id) {
                $sql .= " and cost_center_id = $cost_center_id ";
            }
            if ($username) {
                $sql .= " and username like '%$username%' ";
            }
            // 排序
            $sql .= " order by id desc";
            // 分页
            $desc['page'] = isset($requst['page']) ? (int)$requst['page'] : 1;
            $desc['psize'] = isset($requst['psize']) ? (int)$requst['psize'] : 10;
            $limit_from = ($desc['page'] - 1) * $desc['psize'];
            $sql .= " limit " . $limit_from . "," . $desc['psize'];
            $manager_list = $this->db->query($sql)->result_array();
        }else{
            $sql = "SELECT * FROM app_customer WHERE id = $customer_id";
            if ($username) {
                $sql .= " and username like '%$username%' ";
            }
            $manager_list = $this->db->query($sql)->row_array();
        }
        return $manager_list;
    }

    public function query_user_list($parent_id, $username = '')
    {
        $requst = $_REQUEST;
        $sql = "SELECT * FROM app_customer WHERE identity = 3 AND parent_id = $parent_id";
        if ($username) {
            $sql .= " and username like '%$username%' ";
        }
        // 排序
        $sql .= " order by id desc";
        // 分页
        $desc['page'] = isset($requst['page']) ? (int)$requst['page'] : 1;
        $desc['psize'] = isset($requst['psize']) ? (int)$requst['psize'] : 10;
        $limit_from = ($desc['page'] - 1) * $desc['psize'];
        $sql .= " limit " . $limit_from . "," . $desc['psize'];
        $user_list = $this->db->query($sql)->result_array();

        return $user_list;
    }

    public function add_cost_center($customer_id, $name)
    {
        $data['customer_id'] = $customer_id;
        $data['name'] = $name;
        $data['created_at'] = date('Y-m-d H:i:s', time());
        return $this->db->insert("app_cost_center", $data);

    }
    public function edit_cost_center($customer_id, $name,$cost_center_id)
    {
        $data['customer_id'] = $customer_id;
        $data['name'] = $name;
        $data['created_at'] = date('Y-m-d H:i:s', time());
        return $this->db->where('id',$cost_center_id)->update("app_cost_center", $data);

    }

}